var urltest = sessionStorage.getItem("urltest");
var userId = sessionStorage.getItem("userId");
var fullname = sessionStorage.getItem("fullname");

$(function() {
	if(userId == "" || userId == undefined || userId == null) {
		top.location.href = "login.html";
	} else {
		wuye_name()
		userId = userId;

		var merchantid = "";
		var areaid = "";
		var billstatus = $("#select_type").val();
		var billkind = $("#select_type1").val();
		var paykind = $("#select_type2").val();
		var chargeway = $("#select_type3").val();
		var pageNumber = "1";
		var pageSize = "10";

		var startday = "";
		var endday = "";
		var selectinfo = "";

		//	var userId = "";
		var reque = function() {
			return {
				pageSize: pageSize,
				pageNumber: pageNumber,
				billstatus: billstatus,
				startday: startday,
				endday: endday,
				selectinfo: selectinfo,
				billkind: billkind,
				paykind: paykind,
				merchantid: "",
				areaid: "",
				chargeway: chargeway
			}
		}
		rq1 = reque();
		var req = function() {
			return {
				requestString: "",
				userId: userId,
				fullname: fullname
			}
		}
		requestObj1 = req();
		requestObj1.requestString = JSON.stringify(rq1);
		var req3 = JSON.stringify(requestObj1);
		ajax_data(req3)
	}

});
//加载物业名称
function wuye_name(data) {
	var parentid = data;
	var reque = function() {
		return {
			parentid: parentid,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/property/selectOrganizeNameList',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				console.log(msg);
				console.log(msg.returnString);
				var bb = JSON.parse(msg.returnString);
				//				var str = "";
				var str = '<option value="">' + "请选择物业名称" + '</option>';
				for(var i = 0; i < bb.length; i++) {
					str += "<option value=" + bb[i].organizeid + ">" + bb[i].shortname + "</option>";
				}
				$("#select_wuye").html("");
				$("#select_wuye").append(str);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}
//物业改变查小区
$("#select_wuye").change(function() {
	var scdd_shxz = $("#select_wuye").val();
	xiaoqu_name(scdd_shxz)
})
//加载小区名称
function xiaoqu_name(data) {
	var parentid = data;
	var reque = function() {
		return {
			merchantid: parentid,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/property/selectAreaName',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				console.log(msg);
				console.log(msg.returnString);
				var bb = JSON.parse(msg.returnString);
				//				var str = "";
				var str = '<option value="">' + "请选择小区名称" + '</option>';
				for(var i = 0; i < bb.length; i++) {
					str += "<option value=" + bb[i].areaid + ">" + bb[i].areaname + "</option>";
				}
				$("#select_xiaoqu").html("");
				$("#select_xiaoqu").append(str);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}
//加载数据
function ajax_data(data) {
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectOrderList',
		data: data,
		async: false,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: 1,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}

$("#bp-3-element-test").on("click", function() {
	var aa = $(this).children(".active").children("a").html();
	var pageNumber = aa;
	var pageSize = "10";

	var merchantid = $("#select_wuye").val();
	var areaid = $("#select_xiaoqu").val();
	var billstatus = $("#select_type").val();
	var billkind = $("#select_type1").val();
	var paykind = $("#select_type2").val();
	var chargeway = $("#select_type3").val();

	var star_time = $('.input_1').val();
	var end_time = $('.input_2').val();
	var selectinfo = $("#text_sousuo").val();

	//	var userId = "";
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
			startday: star_time,
			endday: end_time,
			billstatus: billstatus,
			selectinfo: selectinfo,
			billkind: billkind,
			paykind: paykind,
			merchantid: merchantid,
			areaid: areaid,
			chargeway: chargeway
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectOrderList',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				console.log(msg);
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: aa,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});

})

function createShowingTable(bb) {
	//获取后台传过来的jsonData,并进行解析  
	//此处需要让其动态的生成一个table并填充数据 
	if(bb.total == 0) {
		$("#tbbb").html("");
	} else {
		console.log(bb);
		var bb = JSON.parse(bb);
		var tableStr = "";
		var len = bb.length;
		if(len == 0) {
			var wuxiaoxi = '<p>' + "暂无内容" + '</p>'
			$("#tbbb").html(wuxiaoxi);
		} else {
			for(var i = 0; i < len; i++) {
				//				0 未支付 1 已支付 2 已取消
				//				3 已退款 4 缴费失败， 已退款
				if(bb[i].username == undefined) {
					bb[i].username = '';
				}
				if(bb[i].billkindname == undefined) {
					bb[i].billkindname = '';
				}
				if(bb[i].billdate == undefined) {
					bb[i].billdate = '';
				}
				if(bb[i].paymentamt == undefined) {
					bb[i].paymentamt = '';
				}
				if(bb[i].chargeway == 0) {
					bb[i].chargeway = "app";
				} else {
					bb[i].chargeway = "物业后台";
				}
				if(bb[i].issettle == 0) {
					bb[i].issettle = "未结算";
				} else if(bb[i].issettle == 2) {
					bb[i].issettle = "审核中";
				} else {
					bb[i].issettle = "已结算";
				}
				switch(bb[i].billstatus) {
					case 0:
						bb[i].billstatus = "未支付";
						break;
					case 1:
						bb[i].billstatus = "已支付";
						break;
					case 2:
						bb[i].billstatus = "已取消";
						break;
					case 3:
						bb[i].billstatus = "已退款";
						break;
					case 4:
						bb[i].billstatus = "缴费失败， 已退款";
						break;
					case 5:
						bb[i].billstatus = "缴费失败， 未退款";
						break;

				}
				if(bb[i].apipayid == undefined) {
					bb[i].apipayid = ""
				}
				var num = parseInt(i) + parseInt(1);
				tableStr = tableStr + '<tr>' +
					'<th>' + num + '</th>' +
					'<th>' + bb[i].areaname + '</th>' +
					'<th>' + bb[i].rommcode + '</th>' +
					'<th>' + bb[i].billid + '</th>' +
					'<th>' + bb[i].paymentamt + '</th>' +
					'<th>' + bb[i].paykindname + '</th>' +
					'<th>' + bb[i].billdate + '</th>' +
					'<th >' + bb[i].billkindname + '</th>' +
					'<th >' + bb[i].chargeway + '</th>' +
					'<th >' + bb[i].billstatus + '</th>' +
					'<th >' + bb[i].issettle + '</th>' +

					'<th style="display:none" >' + bb[i].apipayid + '</th>' +
					'<th  style="display:none" >' + bb[i].custname + '</th>' +
					'<th  style="display:none" >' + bb[i].goldcoinamt + '</th>' +
					'<th  style="display:none" >' + bb[i].merchantname + '</th>' +
					'<th  style="display:none" >' + bb[i].username + '</th>' +
					'<th  style="color:blue;cursor: pointer" ;><span class="glyphicon glyphicon-edit"></span></th>' +

					'</tr>';
			}
			//将动态生成的table添加的事先隐藏的div中.  
			$("#tbbb").html(tableStr);

		}

		//点击详情
		$("#tbbb tr th:nth-child(17)").click(function() {
			debugger
			$('#myModal').modal('show')
			var apipayid = $(this).siblings().eq(11).html();
			var custname = $(this).siblings().eq(12).html();
			var goldcoinamt = $(this).siblings().eq(13).html();
			var merchantname = $(this).siblings().eq(14).html();
			var username = $(this).siblings().eq(15).html();
			$("#s_id").html(apipayid);
			$("#s_yezhu").html(custname);
			$("#s_wodou").html(goldcoinamt);
			$("#s_wuye").html(merchantname);
			$("#s_huiyuan").html(username);

		});

	}
}

//模糊查询
$('#search_chaxun').click(function() {
	debugger

	var merchantid = $("#select_wuye").val();
	var areaid = $("#select_xiaoqu").val();
	var billstatus = $("#select_type").val();
	var billkind = $("#select_type1").val();
	var paykind = $("#select_type2").val();
	var chargeway = $("#select_type3").val();
	var pageNumber = "1";
	var pageSize = "10";
	var star_time = $('.input_1').val();
	var end_time = $('.input_2').val();
	var selectinfo = $("#text_sousuo").val();

	//	var userId = "";
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
			startday: star_time,
			endday: end_time,
			billstatus: billstatus,
			selectinfo: selectinfo,
			billkind: billkind,
			paykind: paykind,
			merchantid: merchantid,
			areaid: areaid,
			chargeway: chargeway
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	ajax_data(req3)

})


　
function download(filename) {
	$("#export_excel").attr('disabled', true);	
	var merchantid = $("#select_wuye").val();
	var areaid = $("#select_xiaoqu").val();
	var billstatus = $("#select_type").val();
	var billkind = $("#select_type1").val();
	var paykind = $("#select_type2").val();
	var chargeway = $("#select_type3").val();
	var star_time = $('.input_1').val();
	var end_time = $('.input_2').val();
	var selectinfo = $("#text_sousuo").val();
	var reque = function() {
		return {
			startday: star_time,
			endday: end_time,
			billstatus: billstatus,
			selectinfo: selectinfo,
			billkind: billkind,
			paykind: paykind,
			merchantid: merchantid,
			areaid: areaid,
			chargeway: chargeway
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	var url_info = urltest + 'verify/financial/exportOrgainzeOrder';
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	var oReq = new XMLHttpRequest();
	oReq.open("POST", url_info, true);
	oReq.setRequestHeader("Content-type", "application/json;charset=utf-8;");
	oReq.responseType = "blob";
	oReq.onload = function(oEvent) {
		if(oEvent.currentTarget.status == 200 || oEvent.status == 200) {
			$("#export_excel").attr('disabled', false);	
			var content = oReq.response;
			var elink = document.createElement('a');
			elink.download = "物业订单列表.xls";
			elink.style.display = 'none';
			var blob = new Blob([content]);
			elink.href = URL.createObjectURL(blob);
			document.body.appendChild(elink);
			elink.click();
			document.body.removeChild(elink);
		} else {
			alert("导出失败");
			$("#export_excel").attr('disabled', false);	
		}
	};
	oReq.send(req3);
}